package com.example.okcoin;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;

import com.david.service.NotificationUpdateService;
import com.david.service.NotificationUpdateService.IMyService;
import com.example.util.HomeWatcher;
import com.example.util.HomeWatcher.OnHomePressedListener;

public class Launcher extends Activity implements OnHomePressedListener{
	Button b1, b2;
	OkcoinApplication mApplication;
	HomeWatcher mHomeWatcher;
	IMyService myservice; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.launcher);
        findView();
        setListener();
        init();
//        ServiceConnection conn = new ServiceConnection(){
//
//			@Override
//			public void onServiceConnected(ComponentName name, IBinder service) {
//				// TODO Auto-generated method stub
//				 myservice = (IMyService)service;
//			}
//
//			@Override
//			public void onServiceDisconnected(ComponentName name) {
//				// TODO Auto-generated method stub
//				
//			}
//        	
//        };
//        bindService(new Intent(this, NotificationUpdateService.class), conn, Context.BIND_AUTO_CREATE);
        
        IntentFilter intentFilter = new IntentFilter();  
        intentFilter.addAction("com.example.communication.RECEIVER");
	    registerReceiver(new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				String lastbtc = intent.getStringExtra("lastbtc");
				String lastltc = intent.getStringExtra("lastltc");
				b1.setText(lastbtc);
				b2.setText(lastltc);
			}
		}, intentFilter);
	
	}
	
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		mHomeWatcher.stopWatch();
	}
   

	private void init() {
		// TODO Auto-generated method stub
		mApplication = OkcoinApplication.getInstance();
		startService(new Intent(this, 
                NotificationUpdateService.class));
	}
	private void setListener() {
		// TODO Auto-generated method stub
		b1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Launcher.this,
						MainActivity.class);
				Launcher.this.startActivity(intent);
			}
		});
	b2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Launcher.this,
						BtcchinaDepth.class);
				Launcher.this.startActivity(intent);
			}
		});
		
	}
	private void findView() {
		// TODO Auto-generated method stub
		b1 = (Button)findViewById(R.id.launcher_bt1);
		b2 = (Button)findViewById(R.id.launcher_bt2);
	}
	@Override
	public void onHomePressed() {
		mApplication.showNotification();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mApplication.getNotificationManager().cancel(1);
		mHomeWatcher = new HomeWatcher(this);
		mHomeWatcher.setOnHomePressedListener(this);
		mHomeWatcher.startWatch();
		
	}
	@Override
	public void onHomeLongPressed() {
		// TODO Auto-generated method stub
		
	}
	
   

}
